package com.yuyou.activity.comm.api.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.activity.comm.service.PayConfig;
import com.yuyou.activity.comm.service.pay.ZyCreateOrderNotFail;
import com.yuyou.activity.comm.service.pay.ZyOrderAttach;
import com.yuyou.activity.comm.service.tool.MailUtil;
import com.yuyou.activity.util.enums.ZyPayEnums;
import com.yuyou.activity.util.util.HttpClient;
import com.yuyou.activity.util.util.LogUtil;
import com.yuyou.activity.util.util.ZyPaySignUtil;

@RestController
@RequestMapping(value = "/api/pay")
public class PayNoticeController {

	@Value("${appKey}")
	private String appKey;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MailUtil mailUtil;
		
	private Map<String, String> TransMap(HttpServletRequest request) {
		Map<String, String> params = new HashMap<>();
		Enumeration<?> enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String paraName = (String) enu.nextElement();
			params.put(paraName, request.getParameter(paraName));
		}
		logger.info(JSON.toJSONString(params));
		return params;
	}

	@RequestMapping(value = "/order/notice", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String orderNotice(HttpServletRequest request)  {

		Map<String, String> params = TransMap(request);
		String _sign = ZyPaySignUtil.sign(params, appKey);
		if (_sign.equals(params.get("sign"))) {		
			// 路由
			 String attach=params.get("attach");
			 String orderId=params.get("orderId");
			 ZyOrderAttach attachObj=JSONObject.parseObject(attach, new TypeReference<ZyOrderAttach>() {});	
			 String postRes=StringUtils.EMPTY;
			try {
				postRes = HttpClient.post(PayConfig.getConfig(attachObj.getProjectCode()).getNoticeCreateOrderUrl(), params, null);
				LogUtil.info(logger,"success order "+orderId+" notice return"+postRes);
			} catch (Exception e) {
				mailUtil.sendEmail("commOrderNotic", orderId+"");
				LogUtil.info(logger,"error order "+orderId+" notice return"+postRes,e);
			}
			return postRes;
		} else {
			LogUtil.info(logger,"success order notice return"+ZyCreateOrderNotFail
					.getRes(ZyPayEnums.payApplyNoticeCode._4011.getKey()));
			
			return ZyCreateOrderNotFail
					.getRes(ZyPayEnums.payApplyNoticeCode._4011.getKey());
		}		
	}



	@RequestMapping(value = "/refund/notice", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String refundNotice(HttpServletRequest request) {
		Map<String, String> params = TransMap(request);
		String _sign = ZyPaySignUtil.sign(params, appKey);
		if (_sign.equals(params.get("sign"))) {		
			// 路由
			 String attach=params.get("attach");
			 String refundId=params.get("refundId");		 
			 ZyOrderAttach attachObj=JSONObject.parseObject(attach, new TypeReference<ZyOrderAttach>() {});	
			 String postRes=StringUtils.EMPTY;
			try {
				postRes = HttpClient.post(PayConfig.getConfig(attachObj.getProjectCode()).getNoticeRefundOrderUrl(), params, null);
				LogUtil.info(logger,"success refund "+refundId+" notice return"+postRes);
			} catch (Exception e) {
				mailUtil.sendEmail("commRefundNotic", refundId+"");
				LogUtil.info(logger,"error refund "+refundId+" notice return"+postRes,e);
			}
			return postRes;
		} else {
			LogUtil.info(logger,"success refund notice return"+ZyCreateOrderNotFail
					.getRes(ZyPayEnums.payApplyNoticeCode._4011.getKey()));
			
			return ZyCreateOrderNotFail
					.getRes(ZyPayEnums.payApplyNoticeCode._4011.getKey());
		}	
	}

}
